function getUdid(phoneId){
	var um_name = $("input[type=text]").val().trim();  //账号
	var um_pwd = $("input[type=password]").val().trim();  //密码 
	if(um_name.length < 1){
		setLoginPrompt("请输入UM帐号");
		$("input[type=text]").focus();
		$("#J-BtnExit").one("tap", function(){ doLogin(); });
		return;
	}
	if(um_pwd.length < 1){
		setLoginPrompt("请输入UM密码");
		$("input[type=password]").focus();
		$("#J-BtnExit").one("tap", function(){ doLogin(); });
		return;
	}
	var ua1 = navigator.userAgent;
	var phoneType="android";
	if(ua1.indexOf("iPhone")>=0){
		phoneType="ios";
	}else{
		phoneType="android";
	}
	loginPrompt();
    $.ajax({
		url:mpsurl+"/mobile/register/login.do",
		type:"post",
		timeout:120000,
		dataType:"json",
		data:{
			method:"login2",
			um_name:um_name,
			um_pwd:um_pwd,
			phoneId:phoneId,
			phoneType:phoneType
		},
		success:function(data){ 
			var obj = eval(data);
			var resultCode = obj.resultCode;
			if(resultCode=="500700"){
				//保存token
				var ua = navigator.userAgent;
				if(ua.indexOf("iPhone")>=0){
					window.location="/objc://write&token="+obj.token;
				}else{
			    	window.pa_app.SetToken(obj.token);
			    }
				// 获取个人信息并写入cookie
				getUserInfo(obj);
			}else{
				// 重新绑定提交按钮事件并隐藏提交提示框
				$("#J-BtnExit").one("tap", function(){ doLogin(); });
				$("#loginPromptBox").remove();
				setLoginPrompt(obj.resultMessage);
			}
		},  
		error: function (xhr, textStatus, errorThrown) {
			// 重新绑定提交按钮事件并隐藏提交提示框
			$("#J-BtnExit").one("tap", function(){ doLogin(); });
			$("#loginPromptBox").remove();
			setLoginPrompt("网络异常，请检查网络后重试");
		}
	});
}
$(function(){
	
	//检测登录
	var token="";
	//读取token
	var ua = navigator.userAgent;

	if(ua.indexOf("iPhone")>=0){
		window.location="/objc://read";
	}
	/*else{
		token = window.pa_app.GetToken();
		getToken(token);
	}
	*/
	
	//登录
	var $login = $("#J-BtnExit"); 
	$login.one("tap", function(){
		/*
		//安卓关闭键盘
		var ua = navigator.userAgent;
		if(ua.indexOf("iPhone")>=0){
		}else{
			window.pa_app.closeKeyboard();
		}
		*/
		doLogin();
	});
});
// 登录方法
function doLogin(){
	//获取手机唯一标识码
	var ua1 = navigator.userAgent;
	var phoneId ="";
	if(ua1.indexOf("iPhone")>=0){
		window.location="/objc://udid"; 
	}else{
		phoneId = window.pa_app.GetDivId();
		getUdid(phoneId);
	}
}
function getUserInfo(obj){
	$.ajax({
		type:"post",
		url:mpsurl+"/mobile/base/selectUserByEmpId.do",
		success:function(data){
			data = eval("("+data+")");
			if(data.resultCode=="500600"){
				// 把得到的名字和签名写入cookie并跳转
				var userDTO = data.userDTO;
				var nameCn = userDTO.emplName;
				var remark = userDTO.remark;
				var phone = userDTO.phone;
				var imageUrl = userDTO.imageUrl;
				var info = "userRemark="+remark+";userNameCn="+nameCn+";userPhone="+phone+";imageUrl="+imageUrl;
				document.cookie = "indexOfUserInfo="+escape(info);
				//location.href="/mobile/index.html";
				gotoLocation("/mobile/index.html","");
			}else{
				// 重新绑定提交按钮事件并隐藏提交提示框
				$("#J-BtnExit").one("tap", function(){ doLogin(); });
				$("#loginPromptBox").remove();
				setLoginPrompt(data.resultMessage);
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			// 重新绑定提交按钮事件并隐藏提交提示框
			$("#J-BtnExit").one("tap", function(){ doLogin(); });
			$("#loginPromptBox").remove();
			setLoginPrompt("网络异常，请检查网络后重试");
		}
	});
}
function closePrompt(promptInfo){
	$("#GlobalMask").find("p").text(promptInfo);
	$("#GlobalMask").show();
	setTimeout(function(){$("#GlobalMask").hide();},2000);
}
/*
 * 登录提交跳转模态提示
 */
function loginPrompt(){
	var promptBox = "<div id='loginPromptBox' style='position:fixed;left:0;top:0;width:100%;height:100%;z-index:1000;display: block;'>"+
		"<div style='position:absolute;width:100%;height:100%;background-color:#000;opacity:.5;z-index:1000;'></div>"+
		"<div style='position: fixed;left: 30%;top: 40%;width: 40%;z-index: 1001;'>"+
		"<div style='position: absolute;width: 100%;height: 100%;background: #000;-webkit-border-radius: 8px;border-radius: 8px;opacity: .0;z-index: 1;'>"+
		"</div>"+
		"<div style='position: relative;display: -webkit-box;color: #fff;min-height:44px;-webkit-box-orient: horizontal;-webkit-box-pack: center;padding:28px 18px;font-size: 30px;text-align: center;z-index: 2;'>"+
		"<img src='images/loading.gif' width='45px' height='45px'/></div>"+
		"</div>"+
		"</div>";
	$("body").append(promptBox);
}
function setLoginPrompt(msg){
	var obj = document.getElementById("validateBox2");
	if(null == obj){
		var promptBox = 
			"<div id='validateBox2' style='position: fixed;left: 20%;bottom: 15%;width: 60%;z-index: 1000;'>"+
			"<div style='position: absolute;width: 100%;height: 100%;background: #000;-webkit-border-radius: 8px;border-radius: 8px;opacity: .6;z-index: 1;'>"+
			"</div>"+
			"<div style='position: relative;display: -webkit-box;color: #fff;min-height:20px;-webkit-box-orient: horizontal;-webkit-box-pack: center;padding:5px 8px;font-size: 18px;text-align: center;z-index: 2;'>"+
			msg+"</div>"+
			"</div>";
		$("body").append(promptBox);
		// 定时关闭提示框
		setTimeout(function(){
			$("#validateBox2").remove("slow");
		},2000);
	}
}